// 1999-07-08 bkoz

// Copyright (C) 1999, 2003 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 2, or (at your option)
// any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING.  If not, write to the Free
// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
// USA.

// 21.3.5.2 basic_string::append

#include <boost/test/unit_test.hpp>
#include <xpcom/string.hpp>

#ifdef XPCOM_HAS_2BYTE_WCHAR_T

BOOST_AUTO_TEST_CASE(test_append_1_w)
{
  typedef xpcom::wstring::size_type csize_type;
  typedef xpcom::wstring::const_reference cref;
  typedef xpcom::wstring::reference ref;
  csize_type csz01;

  const xpcom::wchar str_lit01[] = L"point bolivar, texas";
  const xpcom::wstring str01(str_lit01);
  const xpcom::wstring str02(L"corpus, ");
  const xpcom::wstring str03;
  xpcom::wstring str05;


  // wstring& append(const wstring&)
  str05 = str02;
  str05.append(str05);
  BOOST_CHECK( str05 == L"corpus, corpus, " );
  str05.append(str01);
  BOOST_CHECK( str05 == L"corpus, corpus, point bolivar, texas" );
  str05.append(str03);
  BOOST_CHECK( str05 == L"corpus, corpus, point bolivar, texas" );
  xpcom::wstring str06;
  str06.append(str05);
  BOOST_CHECK( str06 == str05 );


  // wstring& append(const wstring&, size_type pos, size_type n)
  str05.erase();
  str06.erase();
  csz01 = str03.size();
  try {
    str06.append(str03, csz01 + 1, 0);
    BOOST_CHECK( false );
  }
  catch(std::exception&) {
    BOOST_CHECK( true );
  }
  catch(...) {
    BOOST_CHECK( false );
  }

  csz01 = str01.size();
  try {
    str06.append(str01, csz01 + 1, 0);
    BOOST_CHECK( false );
  }
  catch(std::exception&) {
    BOOST_CHECK( true );
  }
  catch(...) {
    BOOST_CHECK( false );
  }

  str05 = str02;
  str05.append(str01, 0, xpcom::wstring::npos);
  BOOST_CHECK( str05 == L"corpus, point bolivar, texas" );
  BOOST_CHECK( str05 != str02 );

  str06 = str02;
  str06.append(str01, 15, xpcom::wstring::npos);
  BOOST_CHECK( str06 == L"corpus, texas" );
  BOOST_CHECK( str02 != str06 );


  // wstring& append(const xpcom::wchar* s)
  str05.erase();
  str06.erase();
  str05.append(L"");
  BOOST_CHECK( str05 == str03 );

  str05.append(str_lit01);
  BOOST_CHECK( str05 == str01 );

  str06 = str02;
  str06.append(L"corpus, ");
  BOOST_CHECK( str06 == L"corpus, corpus, " );


  // wstring& append(const xpcom::wchar* s, size_type n)
  str05.erase();
  str06.erase();
  str05.append(L"", 0);
  BOOST_CHECK( str05.size() == 0 );
  BOOST_CHECK( str05 == str03 );

  str05.append(str_lit01, sizeof(str_lit01) / sizeof(xpcom::wchar) - 1);
  BOOST_CHECK( str05 == str01 );

  str06 = str02;
  str06.append(L"corpus, ", 6);
  BOOST_CHECK( str06 == L"corpus, corpus" );

  str06 = str02;
  str06.append(L"corpus, ", 12);
  BOOST_CHECK( str06 != L"corpus, corpus, " );


  // wstring& append(size_type n, char c)
  str05.erase();
  str06.erase();
  str05.append(0, L'a');
  BOOST_CHECK( str05 == str03 );
  str06.append(8, L'.');
  BOOST_CHECK( str06 == L"........" );


  // template<typename InputIter>
  //  wstring& append(InputIter first, InputIter last)
  str05.erase();
  str06.erase();
  str05.append(str03.begin(), str03.end());
  BOOST_CHECK( str05 == str03 );

  str06 = str02;
  str06.append(str01.begin(), str01.begin() + str01.find(L'r'));
  BOOST_CHECK( str06 == L"corpus, point boliva" );
  BOOST_CHECK( str06 != str01 );
  BOOST_CHECK( str06 != str02 );

  str05 = str01;
  str05.append(str05.begin(), str05.begin() + str05.find(L'r'));
  BOOST_CHECK( str05 ==  L"point bolivar, texaspoint boliva" );
  BOOST_CHECK( str05 != str01 );
}

#endif
